package com.wanger.swa.dto.buildRecord.req.model;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.func.Func0;
import cn.hutool.core.lang.func.Func1;
import cn.hutool.core.lang.func.LambdaUtil;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: wanger
 * @Date: 2024/10/5 21:37
 * @Description:
 */
@Getter
@AllArgsConstructor
public enum BuildColumnQueryMode {
    //查询模式
    EQ(BuildColumnDTO::getSearchMode, "=", "等于"),
    GT(BuildColumnDTO::getSearchMode, ">", "大于"),
    GTE(BuildColumnDTO::getSearchMode, ">=", "大于等于"),
    LT(BuildColumnDTO::getSearchMode, "<", "小于"),
    LTE(BuildColumnDTO::getSearchMode, "<=", "小于等于"),
    LIKE(BuildColumnDTO::getSearchMode, "like", "模糊查询"),
    BETWEEN(BuildColumnDTO::getSearchMode, "between", "区间"),

    ;

    <T> BuildColumnQueryMode(Func1<T, ?> getFunc, String code, String name) {
        this(LambdaUtil.getFieldName(getFunc), code, name);
    }

    //作用的字段
    private final String field;
    //枚举编码
    private final String code;
    //枚举名称
    private final String name;


    public static final Map<String, List<Map<String, String>>> list = new HashMap<>();
    public static final Map<String, Map<String, BuildColumnQueryMode>> map = new HashMap<>();

    static {
        initialize();
    }

    @SuppressWarnings("all")
    private static void initialize() {
        for (BuildColumnQueryMode value : values()) {
            list.merge(value.field,
                    new ArrayList<Map<String, String>>() {{
                        add(new HashMap<String, String>() {{
                            put("code", value.code);
                            put("name", value.name);
                        }});
                    }}, (l1, l2) -> {
                        l1.addAll(l2);
                        return l1;
                    });
            map.merge(value.field,
                    new HashMap<String, BuildColumnQueryMode>() {{
                        put(value.code, value);
                    }}, (l1, l2) -> {
                        l1.putAll(l2);
                        return l1;
                    });
        }
    }

    public static Map<String, BuildColumnQueryMode> getMapByField(String field) {
        Map<String, BuildColumnQueryMode> enumMap = map.get(field);
        if (CollUtil.isEmpty(enumMap)) throw new RuntimeException("field【" + field + "】没有找到对应的枚举");
        return enumMap;
    }

    public static BuildColumnQueryMode getEnumByFieldAndCode(String field, String code) {
        BuildColumnQueryMode anEnum = getMapByField(field).get(code);
        if (anEnum == null) {
            throw new RuntimeException("field【" + field + "】入参枚举类型【" + code + "】未找到，field【" + field + "】对应的枚举有[" + String.join(",", map.get(field).keySet()) + "]");
        }
        return anEnum;
    }

    public static <T> BuildColumnQueryMode getEnumByFieldAndCode(Func1<T, ?> getFunc, String code) {
        return getEnumByFieldAndCode(LambdaUtil.getFieldName(getFunc), code);
    }

    public static <T> BuildColumnQueryMode getEnumByFieldAndCode(T t, Func1<T, ?> getFunc) {
        return getEnumByFieldAndCode(getFunc, (String) getFunc.callWithRuntimeException(t));
    }

    public static BuildColumnQueryMode getEnumByFieldAndCode(Func0<String> getFunc) {
        return getEnumByFieldAndCode(LambdaUtil.getFieldName(getFunc), getFunc.callWithRuntimeException());
    }
}
